<template>
  <el-container class="home_container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/img/head.jpg" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出登录</el-button>
    </el-header>
    <!-- 主体部分 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '60px' : '240px'">
        <!-- 折叠按钮 -->
        <div class="toggle_button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu background-color="#263238" text-color="#fff" active-text-color="#ffd04b" unique-opened
        :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.path" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i class="el-icon-location"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="item2.path" v-for="item2 in item.children" :key="item2.id" @click="saveDefaultPath(item2.path)">
              <!-- 二级菜单模板区域 -->
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{item2.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主页面 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],

      // 是否折叠
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // 如果请求失败了， 就返回错误消息
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 成功就继续
      this.menuList = res.data
      console.log(res)
    },
    logout() {
      // 销毁token
      window.sessionStorage.clear()
      // 重定向到登录页面
      this.$router.push('/login')
    },

    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接激活状态
    saveDefaultPath(path) {
      window.sessionStorage.setItem('activePath', path)
    }
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}

.el-header {
  background-color: #47aca1;
  display: flex;
  justify-content: space-between; // 左右贴边对齐， 这样logo和文字在左， 退出登录在右
  padding-left: 5px;
  align-items: center; // 按钮顶部和底部留出空隙
  font-size: 20px;
  color: #fff;
  > div {
    //  内部div也使用flex布局
    display: flex;
    align-items: center;
    > img {
      margin-right: 5px;
      height: 52px;
      width: 52px;
      border-radius: 50%;
    }
  }
}
.el-aside {
  background-color: #263238;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.toggle_button {
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
